<?php
include("include/config.inc.php");
$pageName = 'logsheet';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  if(isset($_POST['submitbtn']))
  {
  	$deleteLog = "DELETE FROM logsheet 
  	               WHERE incomingId = ".$_POST['logSheetIncomingId']."
  	                 AND planningId = ".$_POST['logSheetPlanningId'];
  	//$deleteRes = mysql_query($deleteLog);
  	$loopCount = 0;
    while($loopCount < count($_POST['logsheetNo']))
    {
    	$startTime        = date('y-m-d')." ".$_POST['startTimeHour'].":".$_POST['startTimeMinute'].":00";
    	$soakingStartTime = date('y-m-d')." ".$_POST['soakStartTimeHour'].":".$_POST['soakStartTimeMinute'].":00";
    	$endTime          = date('y-m-d')." ".$_POST['endTimeHour'].":".$_POST['endTimeMinute'].":00";
    	
      $incomingId       = ($_POST['logSheetIncomingId'] != 0) ? $_POST['logSheetIncomingId'] : 0;
      $processCount     = ($_POST['processNo'][$loopCount] != 0) ? $_POST['processNo'][$loopCount] : 0;
      $processId        = ($_POST['logSheetprocessId'][$loopCount]   != '') ? $_POST['logSheetprocessId'][$loopCount] : 0;
      $logsheetNo       = ($_POST['logsheetNo'][$loopCount]   != '') ? $_POST['logsheetNo'][$loopCount] : 0;
      $startTemperature = ($_POST['startTemperature'][$loopCount]   != '') ? $_POST['startTemperature'][$loopCount] : 0;
      $endTemperature   = ($_POST['endTemperature'][$loopCount]   != '') ? $_POST['endTemperature'][$loopCount] : 0;
      $quenchingMedia   = ($_POST['quenchingMedia'][$loopCount]   != '') ? $_POST['quenchingMedia'][$loopCount] : 0;
      $quenchingTime    = ($_POST['quenchingTime'][$loopCount]   != '') ? $_POST['quenchingTime'][$loopCount] : 0;
      $tempBforQue      = ($_POST['tempBforQue'][$loopCount]   != '') ? $_POST['tempBforQue'][$loopCount] : 0;
      $tempAfterQue     = ($_POST['tempAfterQue'][$loopCount]   != '') ? $_POST['tempAfterQue'][$loopCount] : 0;
      $status           = ($_POST['status'][$loopCount]   != '') ? $_POST['status'][$loopCount] : -1;
      $logsheetDate     = $_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay'];
  	 
      $insertQueryParty = "INSERT INTO logsheet (incomingId,processId,processNo,planningId,logsheetNo,logsheetDate,startTime,startTemperature,
       																						    soakingStartTime,endTime,endTemperature,qMId,
     																						      quenchingTime,tempBforQue,tempAfterQue,status)
                               VALUES('".$incomingId."','".$processId."',".$processCount.",'".$_POST['logSheetPlanningId']."','".$logsheetNo."',
                              			  '".$logsheetDate."','".$startTime."','".$startTemperature."','".$soakingStartTime."',
                              			  '".$endTime."','".$endTemperature."',".$quenchingMedia.",'".$quenchingTime."','".$tempBforQue."',
                              			  '".$tempAfterQue."','".$status."')";
      $insertQueryPartydResult = mysql_query($insertQueryParty);
      $insertedId = mysql_insert_id();
      if(!$insertQueryPartydResult)
      {
        echo "Insert Query Not Inserted 1: ".mysql_error();
      }

	    $updatePlan = "UPDATE planning 
	                      SET logSheet".$processCount." = ".$insertedId."
	                    WHERE planningId = ".$_POST['logSheetPlanningId'];
	    $updatePlanRes = mysql_query($updatePlan);

      $loopCount++;
    }
    //To update status of log sheet when perticular process's log sheet done : Starts
    if($_POST['processNo'] == 1)
    {
    	$setQry = " logSheet1 = ".$insertedId[1]." ";
    }
    else if($_POST['processNo'] == 2)
    {
    	$setQry = " logSheet1 = ".$insertedId[1].", logSheet2 = ".$insertedId[2]." ";
    }
    else if($_POST['processNo'] == 3)
    {
    	$setQry = " logSheet1 = ".$insertedId[1].", logSheet2 = ".$insertedId[2].", logSheet3 = ".$insertedId[3]." ";
    }
    else
    {
    	$setQry = "NULL";
    }
    //To update status of log sheet when perticular process's log sheet done : Ends
    
    //To check weather log sheet are filled for every records : Starts
    $checkProcess = "SELECT noOfProcess, logSheet1, logSheet2, logSheet3 FROM planning WHERE planningId = ".$_POST['planningId'];
    $checkProcessRes = mysql_query($checkProcess);
    if($row = mysql_fetch_array($checkProcessRes))
    {
    	$noOfOks = 0;
    	if($row['logSheet1'] > 0)
    	{
    		$noOfOks = $noOfOks + 1;
    	}
    	if($row['logSheet2'] > 0)
    	{
    		$noOfOks = $noOfOks + 1;
    	}
    	if($row['logSheet3'] > 0)
    	{
    		$noOfOks = $noOfOks + 1;
    	}        
    	
    	if($row['noOfProcess'] == $noOfOks)
    	{
    		$updatePlanning = "UPDATE planning SET status = 'log complete' WHERE planningId = ".$_POST['planningId'];
    		$updateRes = mysql_query($updatePlanning);
    		
    		$updateIncomming = "UPDATE incoming SET status = 'log complete' WHERE incomingId =".$_POST['planningId'];
        $updateIncommingRes = mysql_query($updateIncomming);
    	}
    	else
    	{
    		$updateIncomming = "UPDATE incoming SET status = 'inlogsheet'WHERE incomingId =".$_POST['logSheetIncomingId'];
        $updateIncommingRes = mysql_query($updateIncomming);
      }
	  }
    //To check weather log sheet are filled for every records : Ends
  }
  $i = 0;
  $incomingRegister = array();
  $selectincomingRegister = "SELECT planning.planningId,planning.incomingId,incoming.partyId,party.partyName,DATE_FORMAT(incoming.IncomingDate,'%d-%m-%Y')AS IncomingDate,
                                    quantity,challan,planning.weight,
                                    processName1.processName AS processName1,processName2.processName AS 
                                    processName2,processName3.processName AS processName3,materialName,hardne
                               FROM planning
                               LEFT JOIN incoming ON planning.incomingId = incoming.incomingId
                               LEFT JOIN party ON party.partyId = planning.partyId
                               LEFT JOIN process AS processName1 ON processName1.processId = planning.processId1
                               LEFT JOIN process AS processName2 ON processName2.processId = planning.processId2
                               LEFT JOIN process AS processName3 ON processName3.processId = planning.processId3
                               LEFT JOIN material ON material.materialId = incoming.materialId
                              WHERE planning.status = 'inlogsheet' ";
  if(isset($_POST['partyId']) && $_POST['partyId'] > 0)                            
  {
	  $selectincomingRegister .= " AND planning.partyId = ".$_POST['partyId']." ";
  }
  $selectincomingRegister .= " ORDER BY planning.planningId ";
  $selectincomingRegisterRes = mysql_query($selectincomingRegister);
  while($incomingRow = mysql_fetch_array($selectincomingRegisterRes))
  {
	  $incomingRegister[$i]['planningId']   = $incomingRow['planningId'];
	  $incomingRegister[$i]['incomingId']   = $incomingRow['incomingId'];
	  $incomingRegister[$i]['partyName']    = $incomingRow['partyName'];
	  $incomingRegister[$i]['IncomingDate'] = $incomingRow['IncomingDate'];
	  $incomingRegister[$i]['qty']          = $incomingRow['quantity'];
	  $incomingRegister[$i]['challan']      = $incomingRow['challan'];
	  $incomingRegister[$i]['weight']       = $incomingRow['weight'];
	  $incomingRegister[$i]['processName1'] = $incomingRow['processName1'];
	  $incomingRegister[$i]['processName2'] = $incomingRow['processName2'];
	  $incomingRegister[$i]['processName3'] = $incomingRow['processName3'];
	  $incomingRegister[$i]['material']     = $incomingRow['materialName'];
	  $incomingRegister[$i]['hardne']       = $incomingRow['hardne'];
	  $i++;
  }
	
	$k = 0;
	$startTime = date('y-m-d H:i:s');
	$catTitleArray = array();
  $select = "SELECT partyId,partyName
               FROM party
              WHERE partyId IN (SELECT incoming.partyId 
                                  FROM incoming
                                  JOIN planning ON incoming.incomingId = planning.incomingId 
                                 WHERE incoming.status != 'outgoing' 
                                   AND incoming.status != 'outgoingDone'
                                   AND planning.status != 'log complete')
              ORDER BY partyName";
  $selectResult = mysql_query($select);
  while($row = mysql_fetch_array($selectResult))
  {
    $catTitleArray['partyId'][$k]   = $row['partyId'];
    $catTitleArray['partyName'][$k] = $row['partyName'];
    $k++;
  }
  
	include("./bottom.php");
	$smarty->assign("msg",'<font size="6">No Record Found</font>');
	$smarty->assign("catTitleArray",$catTitleArray);
	$smarty->assign("incomingRegister",$incomingRegister);
	$smarty->display("logsheet.tpl");
}
?>